import { getToken } from "@/utils/auth"; // get token from cookie
import router from "./router";
import store from '@/store'
const whiteList = ["/login"]; // no redirect whitelist


router.beforeEach(async (to, from, next) => {
  // set page title
  document.title = to.meta.title;

  // determine whether the user has logged in
  const hasToken = getToken();

  if (hasToken) {
    if (to.path === "/login") {
      // if is logged in, redirect to the home page
      next({ path: "/" });
    } else {
      const signKey = store.getters.u_info.root
      if (signKey == 'MX' && ['script-run', 'outbound'].includes(to.name)) {
        next('/');
        return
      }
      next();
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next();
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next(`/login?redirect=${to.path}`);
    }
  }
});
